package com.thm.tfast.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thm.tfast.core.entity.ViewResult;
import com.thm.tfast.modules.sys.model.SysRole;
import com.thm.tfast.modules.sys.model.dto.role.RoleInsertDto;
import com.thm.tfast.modules.sys.model.dto.role.RoleMenuDto;
import com.thm.tfast.modules.sys.model.dto.role.RolePermissionDto;
import com.thm.tfast.modules.sys.model.dto.role.RoleUpdateDto;
import com.thm.tfast.modules.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author THM
 * @since 2022-10-10
 */
@RestController
@Api(tags = "系统：角色管理")
@RequestMapping("/sysRole")
public class SysRoleController {
    
    @Resource
    private SysRoleService sysRoleService;
    
    @ApiOperation("根据条件分页获取角色列表")
    @GetMapping(value = "/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字：角色名称"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true)
    })
    public ViewResult<Page<SysRole>> page(@RequestParam(value = "keyword", required = false) String keyword,
                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysRole> page = sysRoleService.page(keyword, pageNum, pageSize);
        return ViewResult.success(page);
    }

    @ApiOperation("获取指定角色信息")
    @GetMapping("/{roleId}")
    public ViewResult<SysRole> getRoleById(@PathVariable Long roleId) {
        SysRole sysRole = sysRoleService.getById(roleId);
        return ViewResult.success(sysRole);
    }

    @ApiOperation("新增角色信息")
    @PostMapping("/insert")
    public ViewResult<Long> insert(@Validated @RequestBody RoleInsertDto dto){
        return ViewResult.success(sysRoleService.insert(dto));
    }

    @ApiOperation("修改角色信息")
    @PutMapping("/update")
    public ViewResult<String> update(@Validated @RequestBody RoleUpdateDto dto) {
        boolean success = sysRoleService.update(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("禁用或者启用角色")
    @PutMapping("/enableOrDisable/{roleId}")
    public ViewResult<String> enableOrDisable(@PathVariable Long roleId) {
        boolean success = sysRoleService.enableOrDisable(roleId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("删除角色信息")
    @DeleteMapping("/delete/{roleId}")
    public ViewResult<String> delete(@PathVariable Long roleId){
        boolean success = sysRoleService.delete(roleId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("分配角色菜单")
    @PutMapping("/allot/menu")
    public ViewResult<String> allotMenu(@Validated @RequestBody RoleMenuDto dto){
        boolean success = sysRoleService.allotMenu(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("分配角色权限")
    @PutMapping("/allot/permission")
    public ViewResult<String> allotPermission(@Validated @RequestBody RolePermissionDto dto){
        boolean success = sysRoleService.allotPermission(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }
}

